# -*- coding:utf-8 -*-
import mitmproxy
from mitmproxy import http
from mitmproxy.tools._main import mitmdump


class MapLocalEle:
    def request(self, flow: mitmproxy.http.HTTPFlow):
        """
            The full HTTP request has been read.
            可以在request请求中实现 maplocal操作
            实现 map local
            1、创建一个本地文件，设定响应数据
            2、编写脚本，在请求事件中，即给响应对象复制
            3、执行命令
            4、访问浏览器验证结果
        """
        '''https://stock.xueqiu.com/v5/stock/batch/quote.json?_t=1NETEASE55d7e8578966f744d5452096654f6f42.4558458945.1624970447480.1624971747547&_s=250a2c&x=0.51&symbol=SZ300339%2CMF%2CSH600702%2CSZ300059%2CSH601919%2CBABA%2CSH601318&extend=detail'''
        if "quote.json" in flow.request.pretty_url:
            with open("../data/quote.json", encoding="utf-8")as f:
                flow.response = http.HTTPResponse.make(
                    200,
                    f.read(),
                    {"content-type": "application/json"}
                )
addons = [MapLocalEle()] # 实例化

# 方便断点调试
if __name__ == '__main__':
    mitmdump(['-s', __file__, '-p', "8082"])
